package com.qdxwx.data.service.company;

import com.qdxwx.common.config.ProjectConfig;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.company.CompanyMapper;
import com.qdxwx.models.company.Company;
import com.qdxwx.models.company.CompanyAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private CompanyAccountService companyAccountService;


    @Override
    public int insert(Company company) {
        LocalDateTime localDateTime = LocalDateTime.now();
        company.setCreateTime(localDateTime);
        company.setUpdateTime(localDateTime);
        company.setIsDisabled(0);
        int row = companyMapper.insert(company);
        if(row > 0){
            companyAccountService.create(company.getCompanyId());
        }
        return row;
    }

    @Override
    public int update(Company company) {
        company.setUpdateTime(LocalDateTime.now());
        return companyMapper.update(company);
    }

    @Override
    public int delete(int companyId) {
        if (companyId < 1) return 0;
        return companyMapper.deleteById(companyId);
    }

    @Override
    public Company get(int companyId) {
        if (companyId <1 ) return null;
        return companyMapper.getById(companyId);
    }

    @Override
    public Company getByDomain(String domain) {
        return companyMapper.getByDomain(domain);
    }

    @Override
    public Company getByDomain(HttpServletRequest request) {
        String domain = request.getServerName();
        // 缓存 + 后续用Md5查询，解决安全问题，暂未实现
        //String md5Key = TKConvert.toMD5(domain);
        //Company siteCompany = companyMapper.selectByKey(md5Key);
        if (!domain.equals("localhost") && !domain.equals("127.0.0.1")) {
            if (domain.contains(ProjectConfig.getRootDomain())) {
                domain = domain.substring(0, domain.indexOf("."));
            }
            Company company = getByDomain(domain);
            if (company != null) {
                return company;
            }
        }
        return get(1);
    }

    @Override
    public List<Company> getList(Company company, PageRequest page) {
        return companyMapper.getList(company, page);
    }

    @Override
    public long count(Company company) {
        return companyMapper.count(company);
    }

    @Override
    public int updateDisabled(Company company) {
        if(company.getCompanyId() < 1) return 0;
        int row = companyMapper.updateDisabled(company);
        if(row > 0){
            CompanyAccount companyAccount = new CompanyAccount();
            companyAccount.setCompanyId(company.getCompanyId());
            companyAccount.setIsDisabled(company.getIsDisabled());
            companyAccountService.update(companyAccount);
        }
        return row;
    }

    @Override
    public boolean checkDisabled(int companyId) {
        Company company = get(companyId);
        if(company == null) return true;
        return company.getIsDisabled().equals(1);
    }

    @Override
    public boolean checkDisabled(String domain) {
        Company company = getByDomain(domain);
        if(company == null) return true;
        return company.getIsDisabled().equals(1);
    }

    @Override
    public boolean checkDisabled(HttpServletRequest request) {
        Company company = getByDomain(request);
        if(company == null) return true;
        return company.getIsDisabled().equals(1);
    }
}
